Imports System
Imports System.Xml

Public Enum ExecTypes
	NewTrade = 0
	Canceled = 4
	PendingCancel=6
	Rejected=8
	PendingNew = 10	'A
	Trade = 15	'F
	TradeCancel = 17	'H
End Enum

Public Class TradeMsg
	Inherits BaseMsg

	Protected m_rpttype As String	' 0 = Submit, 2 = Accept, 3 = Decline, 6 = Trade Report Cancel
	Protected m_transtype As String	' 0=new, 1=cancel
	Protected m_lastpx As String	' trade price per contract
	Protected m_lastqty As String	' trade quantity

	Protected m_cfi As String	' security type: FXXXX=Future, OPXXXX=Option Put, OCXXXX=Option Call, M=Multi-leg
	Protected m_tmunit As String	'time/term of uom: s=second, min=minute, h=hour, d=day, wk=week, mo=month, yr=year
	Protected m_mmy As String	' maturity month day
	Protected m_strk As String	' strike price
	Protected m_uom As String	' unit of measure - DO WE NEED?

	' if this is a Strip trade (more than 1 month), we will have leg 2 data
	Protected m_leg2mmy As String = ""
	Protected m_leg2strk As String
	Protected m_leg2cfi As String
	Protected m_leg2tmunit As String
	Protected m_leg2uom As String

	Protected m_execid As String
    Protected m_exectype As String
    Protected m_subtype As String

	Protected m_buyer As New ReportSide(RptSides.Buy)
	Protected m_seller As New ReportSide(RptSides.Sell)
	Protected m_excludes As ExcludeFields

#Region "properties"

	Public Property ReportType() As String
		Get
			Return m_rpttype
		End Get
		Set(ByVal Value As String)
			m_rpttype = Value
		End Set
	End Property

	Public Property TransType() As String
		Get
			Return m_transtype
		End Get
		Set(ByVal Value As String)
			m_transtype = Value
		End Set
	End Property

	Public Property TradePrice() As String
		Get
			Return m_lastpx
		End Get
		Set(ByVal Value As String)
			m_lastpx = Value
		End Set
	End Property

	Public Property TradeQuantity() As String
		Get
			Return m_lastqty
		End Get
		Set(ByVal Value As String)
			m_lastqty = Value
		End Set
	End Property

	Public Property FirstCFI() As String
		Get
			Return m_cfi
		End Get
		Set(ByVal Value As String)
			m_cfi = Value
		End Set
	End Property

	Public Property FirstTmUnit() As String
		Get
			Return m_tmunit
		End Get
		Set(ByVal Value As String)
			m_tmunit = Value
		End Set
	End Property

	Public Property FirstMMY() As String
		Get
			Return m_mmy
		End Get
		Set(ByVal Value As String)
			m_mmy = Value
		End Set
	End Property

	Public Property FirstStrikePrice() As String
		Get
			Return m_strk
		End Get
		Set(ByVal Value As String)
			m_strk = Value
		End Set
	End Property

	Public Property FirstUOM() As String
		Get
			Return m_uom
		End Get
		Set(ByVal Value As String)
			m_uom = Value
		End Set
	End Property

	Public Property LastCFI() As String
		Get
			Return m_leg2cfi
		End Get
		Set(ByVal Value As String)
			m_leg2cfi = Value
		End Set
	End Property

	Public Property LastTmUnit() As String
		Get
			Return m_leg2tmunit
		End Get
		Set(ByVal Value As String)
			m_leg2tmunit = Value
		End Set
	End Property

	Public Property LastMMY() As String
		Get
			Return m_leg2mmy
		End Get
		Set(ByVal Value As String)
			m_leg2mmy = Value
		End Set
	End Property

	Public Property LastStrikePrice() As String
		Get
			Return m_leg2strk
		End Get
		Set(ByVal Value As String)
			m_leg2strk = Value
		End Set
	End Property

	Public Property LastUOM() As String
		Get
			Return m_leg2uom
		End Get
		Set(ByVal Value As String)
			m_leg2uom = Value
		End Set
	End Property

	Public Property ExecID() As String
		Get
			Return m_execid
		End Get
		Set(ByVal Value As String)
			m_execid = Value
		End Set
	End Property

	Public Property ExecType() As String
		Get
			Return m_exectype
		End Get
		Set(ByVal Value As String)
			m_exectype = Value
		End Set
	End Property

    Public Property SubType() As String
        Get
            Return m_subtype
        End Get
        Set(ByVal value As String)
            m_subtype = value
        End Set
    End Property

	Public Sub BuyerData(ByVal customerid As String, ByVal clearingid As String, ByVal traderid As String)
		m_buyer.CustomerID = customerid
		m_buyer.ClearingID = clearingid
		m_buyer.TraderID = traderid
	End Sub

	Public Sub SellerData(ByVal customerid As String, ByVal clearingid As String, ByVal traderid As String)
		m_seller.CustomerID = customerid
		m_seller.ClearingID = clearingid
		m_seller.TraderID = traderid
	End Sub

#End Region

	'<FIXML v="4.4" s="20040109" r="20030618">
	'      <TrdCaptRpt RptID="UNIQUE-RPTID-1" TransTyp="0" RptTyp="0" ReqID="Test-ID" LastPx="0.1" LastQty="10000">
	'            <Hdr TID="NYMEX" TSub="cpcapi"/>

	' for non-strip:
    '		<Instrmt ID="LC" CFI="OPXXXX" MMY="200612" Strk="25.00" TmUnit="mo" />
    ' change for non-strip:
    '		<Instrmt ID="LC" CFI="OPXXXX" MMY="200612" Strk="25.00" TmUnit="mo" SubTyp="F-EFS"/>


	' for strip-trade:
    '            <Instrmt ID="LN" CFI="M" TmUnit="mo"/>
    ' change for strip-trade:
    '            <Instrmt ID="LN" CFI="M" TmUnit="mo" SubType="F-STP"/>
    '            <TrdLeg>
	'                  <Leg CFI="OPXXXX" MMY="200802" Strk="5.00"/>
	'            </TrdLeg>
	'            <TrdLeg>
	'                  <Leg CFI="OPXXXX" MMY="200806" Strk="5.00"/>
	'            </TrdLeg>

	'            <RptSide Side="1">
	'                  <Pty R="24" ID="ABNACCT1"/>
	'                  <Pty R="4" ID="444"/>
	'                  <Pty R="12" ID="jjbrady"/>
	'            </RptSide>
	'            <RptSide Side="2">
	'                  <Pty R="24" ID="FIMATACCT"/>
	'                  <Pty R="4" ID="444"/>
	'                  <Pty R="12" ID="cornwelld"/>
	'            </RptSide>
	'      </TrdCaptRpt>
	'</FIXML>
	'<FIXML v="4.4" s="20040109" r="20030618">
	'      <TrdCaptRptAck RptID="UNIQUE-RPTID-1" TransTyp="0" TrdRptStat="0" ExecID="47102" ExecTyp="A">
	'            <Hdr SID="NYMEX" SSub="cpcapi"/>
	'            <Instrmt ID="LN" CFI="M" TmUnit="mo"/>
	'            <TrdLeg>
	'                  <Leg CFI="OPXXXX" MMY="200802" Strk="5.00"/>
	'            </TrdLeg>
	'            <TrdLeg>
	'                  <Leg CFI="OPXXXX" MMY="200806" Strk="5.00"/>
	'            </TrdLeg>
	'      </TrdCaptRptAck>
	'</FIXML>
	Public Sub New()
		IsCPC = False
		ReportType = "0"
		TransType = "0"
		ExecID = ""
		RequestType = "TrdCaptRpt"
		ResponseType = "TrdCaptRptAck"
	End Sub

    Private Sub WriteOrExclude(ByRef xmlW As XmlTextWriter, ByVal field As String, ByVal val As String, Optional ByVal ttype As String = "non")
        If m_excludes.IsExcluded(field) Then
            Exit Sub
        End If

        'pla20080303 - new code to check for default values for a field. specifically, SubTyp at this point
        ' if there is no default value, then exclude the field at this point
        If val = "" And field = "SubTyp" Then
            val = Globals.checkForDefault(ProductID, field, ttype)

            If val = "" Then
                Exit Sub
            End If
        End If
        xmlW.WriteAttributeString(field, val)
    End Sub

    Protected Overrides Function RequestMsg(ByRef XmlW As XmlTextWriter) As Boolean

        m_excludes = New ExcludeFields(Me.ProductID)

        XmlW.WriteStartElement("FIXML")
        XmlW.WriteAttributeString("v", "4.4")
        XmlW.WriteAttributeString("s", "20040109")
        XmlW.WriteAttributeString("r", "20030618")

        '      <TrdCaptRpt RptID="UNIQUE-RPTID-1" TransTyp="0" RptTyp="0" ReqID="Test-ID" LastPx="0.1" LastQty="10000">

        XmlW.WriteStartElement("TrdCaptRpt")
        XmlW.WriteAttributeString("RptID", UID)
        XmlW.WriteAttributeString("TransTyp", TransType)
        XmlW.WriteAttributeString("RptTyp", ReportType)
        XmlW.WriteAttributeString("ReqID", UID)
        WriteOrExclude(XmlW, "LastPx", TradePrice)
        WriteOrExclude(XmlW, "LastQty", TradeQuantity)
        If ExecID <> "" Then
            XmlW.WriteAttributeString("ExecID", ExecID)
        End If

        '            <Hdr TID="NYMEX" TSub="cpcapi"/>
        XmlW.WriteStartElement("Hdr")
        XmlW.WriteAttributeString("TID", "NYMEX")
        XmlW.WriteAttributeString("TSub", "cpcapi")
        XmlW.WriteEndElement()

        If TransType = "1" Then
            ' this is a cancel request, we don't need more data
            XmlW.WriteEndElement()          'TrdCaptRpt
            XmlW.WriteEndElement()          'FIXML
            Return True
        End If

        If LastMMY = "" Then
            ' for non-strip:
            '		<Instrmt ID="LC" CFI="OPXXXX" MMY="200612" Strk="25.00" TmUnit="mo" />
            XmlW.WriteStartElement("Instrmt")
            WriteOrExclude(XmlW, "ID", m_prodid)
            WriteOrExclude(XmlW, "CFI", FirstCFI)
            WriteOrExclude(XmlW, "MMY", FirstMMY)
            WriteOrExclude(XmlW, "Strk", FirstStrikePrice)
            WriteOrExclude(XmlW, "TmUnit", FirstTmUnit)
            WriteOrExclude(XmlW, "SubTyp", SubType)
            XmlW.WriteEndElement()
        Else
            ' for strip-trade:
            '            <Instrmt ID="LN" CFI="M" TmUnit="mo"/>
            '            <TrdLeg>
            '                  <Leg CFI="OPXXXX" MMY="200802" Strk="5.00"/>
            '            </TrdLeg>
            '            <TrdLeg>
            '                  <Leg CFI="OPXXXX" MMY="200806" Strk="5.00"/>
            '            </TrdLeg>
            XmlW.WriteStartElement("Instrmt")
            WriteOrExclude(XmlW, "ID", m_prodid)
            WriteOrExclude(XmlW, "CFI", "M")
            WriteOrExclude(XmlW, "TmUnit", FirstTmUnit)
            WriteOrExclude(XmlW, "SubTyp", SubType, "")
            XmlW.WriteEndElement()

            XmlW.WriteStartElement("TrdLeg")
            XmlW.WriteStartElement("Leg")
            WriteOrExclude(XmlW, "CFI", FirstCFI)
            WriteOrExclude(XmlW, "MMY", FirstMMY)
            WriteOrExclude(XmlW, "Strk", FirstStrikePrice)
            XmlW.WriteEndElement()
            XmlW.WriteEndElement()

            XmlW.WriteStartElement("TrdLeg")
            XmlW.WriteStartElement("Leg")
            WriteOrExclude(XmlW, "CFI", LastCFI)
            WriteOrExclude(XmlW, "MMY", LastMMY)
            WriteOrExclude(XmlW, "Strk", LastStrikePrice)
            XmlW.WriteEndElement()
            XmlW.WriteEndElement()
        End If

        m_buyer.BuildXML(XmlW)
        m_seller.BuildXML(XmlW)

        XmlW.WriteEndElement()       'TrdCaptRpt
        XmlW.WriteEndElement()       'FIXML

        Return True
    End Function

	Protected Overrides Sub ResponseParse(ByRef node As XmlNode)
		m_exectype = node.Attributes("ExecTyp").InnerText.ToString

		If Not node.Attributes("Txt") Is Nothing Then
			m_errmsg = node.Attributes("Txt").InnerText.ToString
			Exit Sub
		End If

		m_execid = node.Attributes("ExecID").InnerText.ToString
	End Sub

End Class
